﻿<?php

	session_start();
	require_once('../config.php');
	include_once("../database/DataBase.php");
	include_once("function.php");

	$pid = $_POST["pid"];
	$cid = $_POST["cid"];
	$p_code = $_POST["p_code"];
	$p_cost = $_POST["p_cost"];
	$p_price = $_POST["p_price"];
	$p_category = $_POST["p_category"];
	$p_storein = $_POST["p_storein"];
	$p_expire = $_POST["p_expire"];
	$p_quantity = $_POST["p_quantity"];
	$p_status = $_POST["p_status"];
	
	$p_desc = $_POST["p_desc"];
	
	$database = New DataBase();
	$database->open_connection();
	
	$uploads_dir = "../files/".$cid."/";
	If(!file_exists($uploads_dir)){
		mkdir($uploads_dir);
	}

	for($i=0;$i<count($_FILES['p_image']);$i++) {
		if ($_FILES['p_image']['name'][$i] > '') { 
			$new_location	="";
			$new_file_name	=$cid."".date("YmdHis").rand().".".substr(strrchr($_FILES['p_image']['name'][$i],'.'),1);
			$tmp			=$_FILES['p_image']['tmp_name'][$i];
			$new_location	=$uploads_dir.$new_file_name;
			$p_image[$i]	=$new_location;

			copy($_FILES['p_image']['tmp_name'][$i],$new_location);
		}
	}
	
	$result = $database->updateProduct($pid, $cid, $p_code, $p_cost, $p_price, $p_category, $p_storein, $p_expire, $p_quantity, $p_status,$p_name, $p_desc, $p_image);
	
	if(!is_null($result)){
		$database->LogAction($_SESSION["login_id"], "Update", "Update product ".$pid." from ".getIpAddress());
		$_SESSION["message"] = "Update Product success ( Product Code: ".$p_code." )";
		header('Location: ../Message.php');
	}
	
	$database->close_connection();
?>